home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 2000
/
MacHack 2000.toast
/
pc
/
The Hacks
/
Close Window Thing
/
Close Window Thing Setup
/
Close Window Thing Setup.c
next >
Wrap
C/C++ Source or Header
|
2000-06-23
|
11KB
|
462 lines
#define rMenuBar 128
#define mApple 128
#define mFile 129
#define iAbout 1
#define iQuit 1
typedef struct
{
short effectType;
short speed;
} Preferences;
void InitializeToolbox( void );
void DrawMenubar( void );
void EventOnce( void );
void DrawMenubar( void );
void SetUpMainWindow( void );
void HandleUpdate( EventRecord theEvent );
void HandleMouseDown( EventRecord *eventPtr );
void HandleAppleChoice( short item );
void HandleFileChoice( short item );
void HandleKeyDown( EventRecord theEvent );
void HandleMenuChoice( long theChoice );
void AboutDialog( void );
void DoAddPane( Str255 title );
void CopyString( Str255 to, Str255 from );
void OpenPrefs( Preferences *prefs );
void NewPrefs( Preferences *prefs, FSSpec spec );
void SavePrefs( Preferences *prefs );
void ClosePrefs( void );
Boolean done = false;
WindowPtr mainWindow;
ControlHandle rootControl, tabControl, pane[2], effectGroup, effect1, effect2, speed;
ControlHandle speedLabel, slowLabel, fastLabel;
Preferences **prefsHan, prefs;
short activePane = 1, prefsFRefNum;
void main( void )
{
InitializeToolbox();
OpenPrefs( &prefs );
SetUpMainWindow();
DrawMenubar();
SetControlValue( speed, prefs.speed );
if ( prefs.effectType != 0 )
SetControlValue( effectGroup, 1 );
else
{
DeactivateControl( effect1 );
DeactivateControl( effect2 );
SetControlValue( effect1, 1 );
}
if ( prefs.effectType == 1 )
SetControlValue( effect1, 1 );
else if ( prefs.effectType == 2 )
SetControlValue( effect2, 1 );
do
{
EventOnce();
}
while ( done == false );
prefs.speed = GetControlValue( speed );
prefs.effectType = GetControlValue( effectGroup );
if ( prefs.effectType == 1 )
{
prefs.effectType = GetControlValue( effect1 );
if ( prefs.effectType == 0 )
prefs.effectType = 2;
}
SavePrefs( &prefs );
ClosePrefs();
KillControls( mainWindow );
DisposeWindow( mainWindow );
}
void InitializeToolbox( void )
{
InitGraf( &qd.thePort );
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs( 0L );
FlushEvents( everyEvent, 0 );
InitCursor();
}
void DrawMenubar( void )
{
Handle menuBar;
MenuHandle menu;
menuBar= GetNewMBar( rMenuBar );
SetMenuBar( menuBar );
DrawMenuBar();
menu = GetMenuHandle( mApple );
AppendResMenu( menu, 'DRVR' );
}
void SetUpMainWindow( void )
{
Rect windRect = {100,100,320,400}, tabRect, effectGroupRect, effectRect, speedRect;
Str255 title = "\pEffect Speed:";
mainWindow = NewCWindow( nil, &windRect, "\pClose Window Thing Setup", false, documentProc, 0L, false, nil);
CreateRootControl( mainWindow, &rootControl );
SetRect( &tabRect, -1, 10, mainWindow->portRect.right + 1, mainWindow->portRect.bottom + 1 );
tabControl = NewControl( mainWindow, &tabRect, "\pTab", true, kControlSupportsEmbedding, 1, 0, kControlTabLargeProc, -1 );
SetControlValue( tabControl, 0 );
DoAddPane( "\pGeneral" );
speedRect.top = (*pane[0])->contrlRect.top + 12;
speedRect.left = (*pane[0])->contrlRect.left + 12;
speedRect.bottom = speedRect.top + 16;
speedRect.right = (*pane[0])->contrlRect.right - 12;
speedLabel = NewControl( mainWindow, &speedRect, "\pEffect Speed:", true, 0, 0, 0, kControlStaticTextProc, -1 );
SetControlData( speedLabel, 0, kControlStaticTextTextTag, title[0], (Ptr)&title[1] );
EmbedControl( speedLabel, pane[0] );
speedRect.top = speedRect.top + 18;
speedRect.bottom = speedRect.top + 16;
speed = NewControl( mainWindow, &speedRect, "\pSpeed", true, 1, 1, 8, kControlSliderProc + kControlSliderHasTickMarks, -1 );
EmbedControl( speed, pane[0] );
DoAddPane( "\pEffect" );
effectGroupRect = (*pane[1])->contrlRect;
effectGroupRect.top += 12;
effectGroupRect.left += 12;
effectGroupRect.bottom -= 12;
effectGroupRect.right -= 12;
effectGroup = NewControl( mainWindow, &effectGroupRect, "\pEnable Close Window Things", true, kControlSupportsEmbedding, 1, 0, kControlGroupBoxCheckBoxProc, -1 );
SetControlValue( tabControl, 0 );
EmbedControl( effectGroup, pane[1] );
effectRect = effectGroupRect;
effectRect.top += 30;
effectRect.left += 12;
effectRect.bottom = effectRect.top + 12;
effectRect.right = effectRect.left + 80;
effect1 = NewControl( mainWindow, &effectRect, "\pStarburst", true, kControlRadioButtonUncheckedValue, kControlRadioButtonUncheckedValue, kControlRadioButtonCheckedValue, kControlRadioButtonProc, -1 );
EmbedControl( effect1, effectGroup );
OffsetRect( &effectRect, 0, 17 );
effect2 = NewControl( mainWindow, &effectRect, "\pSlider", true, kControlRadioButtonUncheckedValue, kControlRadioButtonUncheckedValue, kControlRadioButtonCheckedValue, kControlRadioButtonProc, -1 );
EmbedControl( effect2, effectGroup );
HideControl( pane[1] );
ShowWindow( mainWindow );
}
void EventOnce( void )
{
EventRecord event;
WaitNextEvent( everyEvent, &event, 0x7FFFFFFF, nil );
switch ( event.what )
{
case mouseDown:
HandleMouseDown( &event );
break;
case keyDown:
HandleKeyDown( event );
break;
case autoKey:
HandleKeyDown( event );
break;
case updateEvt:
HandleUpdate( event );
break;
case activateEvt:
break;
}
}
void HandleUpdate( EventRecord theEvent )
{
WindowPtr window;
window = (WindowPtr)theEvent.message;
BeginUpdate( window );
SetPort( window );
DrawControls( window );
EndUpdate( window );
}
void HandleMouseDown( EventRecord *eventPtr )
{
WindowPtr whichWindow;
Rect dragRect = {0,0,3000, 3000};
Point thePoint;
ControlHandle theControl;
short thePart;
long menuChoice;
thePart = FindWindow( eventPtr->where, &whichWindow );
thePoint = (*eventPtr).where;
switch (thePart)
{
case inSysWindow:
SystemClick( eventPtr, whichWindow );
break;
case inMenuBar:
menuChoice = MenuSelect( eventPtr->where );
if ( menuChoice != 0 )
HandleMenuChoice( menuChoice );
case inDrag:
DragWindow( whichWindow, eventPtr->where, &dragRect);
break;
case inGoAway:
if ( TrackGoAway(whichWindow, eventPtr->where) == true)
DisposeWindow(whichWindow);
break;
case inContent:
SetPort( whichWindow );
GlobalToLocal( &thePoint );
theControl = FindControlUnderMouse( thePoint, whichWindow, &thePart );
if ( theControl == nil )
break;
thePart = HandleControlClick( theControl, thePoint, (*eventPtr).modifiers, (ControlActionUPP)-1L );
if ( theControl == tabControl && thePart != activePane && thePart != 0 )
{
HideControl( pane[activePane - 1] );
DrawControls( whichWindow );
ShowControl( pane[thePart - 1] );
activePane = thePart;
}
else if ( theControl == effectGroup && thePart != 0 )
{
if ( GetControlValue( theControl ) == 1 )
{
SetControlValue( theControl, 0 );
DeactivateControl( effect1 );
DeactivateControl( effect2 );
}
else
{
SetControlValue( theControl, 1 );
ActivateControl( effect1 );
ActivateControl( effect2 );
}
}
else if ( theControl == effect1 )
{
SetControlValue( theControl, 1 );
SetControlValue( effect2, 0 );
}
else if ( theControl == effect2 )
{
SetControlValue( theControl, 1 );
SetControlValue( effect1, 0 );
}
break;
}
HiliteMenu( 0 );
}
void HandleAppleChoice( short item )
{
MenuHandle appleMenu;
Str255 accName;
short accNumber;
switch ( item )
{
case iAbout:
AboutDialog();
break;
default:
appleMenu = GetMenuHandle( mApple );
GetMenuItemText( appleMenu, item, accName );
accNumber = OpenDeskAcc( accName );
break;
}
}
void HandleFileChoice( short item )
{
switch ( item )
{
case iQuit:
done = true;
break;
}
}
void HandleKeyDown( EventRecord theEvent )
{
short theChar;
long theChoice;
theChar = theEvent.message & charCodeMask;
if ( ( theEvent.modifiers & cmdKey ) != 0 )
{
if ( theEvent.what != autoKey )
{
theChoice = MenuKey( theChar );
HandleMenuChoice( theChoice );
}
}
}
void HandleMenuChoice( long theChoice )
{
short theMenu;
short theMenuItem;
theMenu = HiWord( theChoice );
theMenuItem = LoWord( theChoice );
switch ( theMenu )
{
case mApple:
HandleAppleChoice( theMenuItem );
break;
case mFile:
HandleFileChoice( theMenuItem );
break;
}
HiliteMenu( 0 );
}
void AboutDialog( void )
{
GrafPtr savePort;
DialogPtr dlog;
short item;
GetPort( &savePort );
dlog = GetNewDialog( 128, 0L, (WindowPtr) -1L );
SetDialogDefaultItem( dlog, 1 );
SetPort( dlog );
if ( dlog )
{
do
{
ModalDialog( nil, &item );
}
while ( item != 1 );
DisposeDialog(dlog);
}
SetPort( savePort );
}
void DoAddPane( Str255 title )
{
ControlTabInfoRec theTabInfo;
Rect paneRect;
short tabCount = GetControlMaximum( tabControl ) + 1;
GetControlData( tabControl, tabCount, kControlTabContentRectTag, sizeof( Rect ), (Ptr)&paneRect, nil );
theTabInfo.version = 0;
theTabInfo.iconSuiteID = 0;
SetControlMaximum( tabControl, tabCount );
CopyString( theTabInfo.name, title );
SetControlData( tabControl, tabCount, kControlTabInfoTag, sizeof( ControlTabInfoRec ), (Ptr)&theTabInfo );
pane[tabCount - 1] = NewControl( mainWindow, &paneRect, "\pTab", true, kControlSupportsEmbedding, 0, 0, kControlUserPaneProc, -1 );
EmbedControl( pane[tabCount - 1], tabControl );
SetControlValue( pane[tabCount - 1], 0 );
}
void CopyString( Str255 to, Str255 from )
{
short times = 0;
do
{
to[times] = from[times];
times++;
}
while ( times < from[0]+1 );
}
void OpenPrefs( Preferences *prefs )
{
OSErr myErr;
FSSpec mySpec;
short myVRef;
long myDirID;
myErr = FindFolder(0x8000, 'pref', false, &myVRef, &myDirID);
myErr = FSMakeFSSpec( myVRef, myDirID, "\pClose Window Thing Prefs", &mySpec );
if( myErr == fnfErr )
NewPrefs( prefs, mySpec );
prefsFRefNum = FSpOpenResFile( &mySpec,3 );
prefsHan = (Preferences **)GetResource( 'Pref',128 );
prefs->effectType = (**prefsHan).effectType;
prefs->speed = (**prefsHan).speed;
}
void NewPrefs( Preferences *prefs, FSSpec spec )
{
FSpCreateResFile( &spec, 'PEJ2', 'pref', -1 );
prefsFRefNum = FSpOpenResFile( &spec, 3 );
prefs->effectType = 1;
prefs->speed = 1;
(Handle)prefsHan = NewHandle( sizeof( Preferences ) );
(**prefsHan).effectType = prefs->effectType;
(**prefsHan).speed = prefs->speed;
HLock( (Handle)prefsHan );
AddResource( (Handle)prefsHan, 'Pref', 128, "\pPrefs");
WriteResource( (Handle)prefsHan );
HUnlock( (Handle)prefsHan );
UpdateResFile( prefsFRefNum );
}
void SavePrefs( Preferences *prefs )
{
(**prefsHan).effectType = prefs->effectType;
(**prefsHan).speed = prefs->speed;
ChangedResource( (Handle)prefsHan );
UpdateResFile( prefsFRefNum );
}
void ClosePrefs( void )
{
if ( prefsHan != nil )
ReleaseResource( (Handle)prefsHan );
if ( prefsFRefNum )
FSClose( prefsFRefNum );
}